<template>
  <div class="hero-leveling">
    <h2>🧙‍♂️ Vue 法师成长记</h2>
    
    <div class="hero-card">
      <div class="hero-avatar">🧙‍♂️</div>
      <div class="hero-info">
        <h3>{{ name }} Lv.{{ level }}</h3>
        <div class="level-bar">
          <div class="level-fill" :style="{ width: (level / 10) * 100 + '%' }"></div>
        </div>
        <p class="level-text">{{ level }}/10</p>
      </div>
    </div>
    
    <div class="skills-section">
      <h4>🌟 技能列表 ({{ skills.length }}个)</h4>
      <div class="skills-grid">
        <div v-for="(skill, index) in skills" :key="index" class="skill-item">
          <span class="skill-icon">{{ getSkillIcon(skill) }}</span>
          <span class="skill-name">{{ skill }}</span>
        </div>
      </div>
    </div>
    
    <div class="actions">
      <button @click="levelUp" :disabled="level >= 10" class="level-up-btn">
        {{ level >= 10 ? '🏆 已达到最高等级' : '⚔️ 打怪升级' }}
      </button>
      
      <button @click="resetHero" class="reset-btn">
        🔄 重新开始
      </button>
    </div>
    
    <div class="level-rewards">
      <h4>🎯 等级奖励</h4>
      <div class="reward-list">
        <div 
          v-for="reward in levelRewards" 
          :key="reward.level"
          :class="['reward-item', { 'unlocked': level >= reward.level }]"
        >
          <span class="reward-level">Lv.{{ reward.level }}</span>
          <span class="reward-skill">{{ reward.skill }}</span>
          <span class="reward-status">{{ level >= reward.level ? '✅' : '🔒' }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs } from 'vue'

// 定义英雄类型接口
interface Hero {
  name: string
  level: number
  skills: string[]
}

// 定义等级奖励接口
interface LevelReward {
  level: number
  skill: string
}

// 创建响应式英雄对象
const hero = reactive<Hero>({
  name: 'Vue 法师',
  level: 1,
  skills: ['火球术']
})

// 安全解构（保持响应性）
const { name, level, skills } = toRefs(hero)

// 等级奖励配置
const levelRewards = reactive<LevelReward[]>([
  { level: 1, skill: '火球术' },
  { level: 3, skill: '冰冻术' },
  { level: 5, skill: '雷电术' },
  { level: 7, skill: '治愈术' },
  { level: 9, skill: '传送术' },
  { level: 10, skill: '大招：Vue风暴' }
])

// 获取技能图标
const getSkillIcon = (skill: string): string => {
  const iconMap: Record<string, string> = {
    '火球术': '🔥',
    '冰冻术': '❄️',
    '雷电术': '⚡',
    '治愈术': '💚',
    '传送术': '🌀',
    '大招：Vue风暴': '🌪️'
  }
  return iconMap[skill] || '✨'
}

// 升级函数
const levelUp = () => {
  if (level.value >= 10) return
  
  level.value++
  
  // 检查是否解锁新技能
  const newReward = levelRewards.find(reward => reward.level === level.value)
  if (newReward && !skills.value.includes(newReward.skill)) {
    skills.value.push(newReward.skill)
    console.log(`🎉 恭喜！解锁新技能：${newReward.skill}`)
  }
  
  console.log(`📈 升级成功！当前等级：${level.value}`)
}

// 重置英雄
const resetHero = () => {
  hero.name = 'Vue 法师'
  hero.level = 1
  hero.skills = ['火球术']
  console.log('🔄 英雄已重置')
}
</script>

<style scoped>
.hero-leveling {
  max-width: 600px;
  margin: 30px auto;
  padding: 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 20px;
  color: white;
  font-family: 'Arial', sans-serif;
}

.hero-card {
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.1);
  padding: 20px;
  border-radius: 15px;
  margin: 20px 0;
}

.hero-avatar {
  font-size: 60px;
  margin-right: 20px;
}

.hero-info {
  flex: 1;
}

.hero-info h3 {
  margin: 0 0 10px 0;
  font-size: 24px;
}

.level-bar {
  width: 100%;
  height: 8px;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 4px;
  overflow: hidden;
  margin: 10px 0;
}

.level-fill {
  height: 100%;
  background: linear-gradient(90deg, #42b883, #369870);
  transition: width 0.5s ease;
  border-radius: 4px;
}

.level-text {
  font-size: 12px;
  margin: 0;
  opacity: 0.8;
}

.skills-section {
  margin: 30px 0;
}

.skills-section h4 {
  margin-bottom: 15px;
  font-size: 18px;
}

.skills-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
}

.skill-item {
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.1);
  padding: 10px;
  border-radius: 8px;
  transition: transform 0.2s ease;
}

.skill-item:hover {
  transform: translateY(-2px);
}

.skill-icon {
  font-size: 20px;
  margin-right: 8px;
}

.skill-name {
  font-size: 14px;
}

.actions {
  display: flex;
  gap: 15px;
  margin: 30px 0;
}

.level-up-btn, .reset-btn {
  flex: 1;
  padding: 15px;
  font-size: 16px;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: bold;
}

.level-up-btn {
  background: #42b883;
  color: white;
}

.level-up-btn:hover:not(:disabled) {
  background: #369870;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.level-up-btn:disabled {
  background: #cccccc;
  cursor: not-allowed;
  transform: none;
}

.reset-btn {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.reset-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.level-rewards {
  margin-top: 30px;
}

.level-rewards h4 {
  margin-bottom: 15px;
  font-size: 18px;
}

.reward-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.reward-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(255, 255, 255, 0.1);
  padding: 12px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.reward-item.unlocked {
  background: rgba(66, 184, 131, 0.2);
  border: 1px solid rgba(66, 184, 131, 0.5);
}

.reward-level {
  font-weight: bold;
  font-size: 14px;
  min-width: 40px;
}

.reward-skill {
  flex: 1;
  text-align: left;
  margin: 0 15px;
  font-size: 14px;
}

.reward-status {
  font-size: 16px;
}
</style>